/*
 * @Author: your name
 * @Date: 2020-07-24 23:44:01
 * @LastEditTime2020-12-02 22:32:57
 * @LastEditorsPlease set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \STM32F103_FreeRTOS\User\app\shell\shell_queue.c
 */ 
#include "shell_queue.h"

static void shell_queue_init(struct ShellQueue *pQueue);
static boolean shell_queue_isfull(struct ShellQueue *pQueue);
static boolean shell_queue_isempty(struct ShellQueue *pQueue);
static boolean shell_queue_push(struct ShellQueue *pQueue, uint8_t u8Data);
static boolean shell_queue_pop(struct ShellQueue *pQueue, uint8_t *pu8Data);


static uint8_t s_au8ShellQueueDataISR[SHELL_QUEUE_SIZE];
ShellQueue_t g_ShellQueueISR = 
{
    .u16Head  = 0u,
    .u16Tail  = SHELL_QUEUE_SIZE - 1u,
    .u16Numb  = 0u,
    .cu16Size = SHELL_QUEUE_SIZE,
    .pu8Data  = s_au8ShellQueueDataISR,
    .Init     = shell_queue_init,
    .IsFull   = shell_queue_isfull,
    .IsEmpty  = shell_queue_isempty,
    .Push     = shell_queue_push,
    .Pop      = shell_queue_pop,
};

static uint8_t s_au8ShellQueueData[SHELL_QUEUE_SIZE];
ShellQueue_t g_ShellQueue = 
{
    .u16Head  = 0u,
    .u16Tail  = SHELL_QUEUE_SIZE - 1u,
    .u16Numb  = 0u,
    .cu16Size = SHELL_QUEUE_SIZE,
    .pu8Data  = s_au8ShellQueueData,
    .Init     = shell_queue_init,
    .IsFull   = shell_queue_isfull,
    .IsEmpty  = shell_queue_isempty,
    .Push     = shell_queue_push,
    .Pop      = shell_queue_pop,
};

static void shell_queue_init(struct ShellQueue *pQueue)
{
    pQueue->u16Head  = 0u;
    pQueue->u16Tail  = SHELL_QUEUE_SIZE - 1u;
    pQueue->u16Numb  = 0u;
}

static boolean shell_queue_isfull(struct ShellQueue *pQueue)
{
    boolean bRet = FALSE;
    if(pQueue->cu16Size == pQueue->u16Numb)
    {
        bRet = TRUE;
    }
    return bRet;
}

static boolean shell_queue_isempty(struct ShellQueue *pQueue)
{
    boolean bRet = FALSE;
    if(0u == pQueue->u16Numb)
    {
        bRet = TRUE;
    }
    return bRet;
}

static boolean shell_queue_push(struct ShellQueue *pQueue, uint8_t u8Data)
{
    boolean bRet = FALSE;
    if(pQueue->IsFull(pQueue))
    {
        bRet = FALSE;
    }
    else
    {
        pQueue->u16Tail++;
        if(pQueue->u16Tail >= SHELL_QUEUE_SIZE)
        {
            pQueue->u16Tail = 0u;
        }
        pQueue->pu8Data[pQueue->u16Tail] = u8Data;
        pQueue->u16Numb++;
        bRet = TRUE;
    }
    return bRet;
}

static boolean shell_queue_pop(struct ShellQueue *pQueue, uint8_t *pu8Data)
{
    boolean bRet = FALSE;
    if(pQueue->IsEmpty(pQueue))
    {
        bRet = FALSE;
    }
    else
    {
        *pu8Data = pQueue->pu8Data[pQueue->u16Head];
        pQueue->u16Head++;
        if(pQueue->u16Head >= SHELL_QUEUE_SIZE)
        {
            pQueue->u16Head = 0u;
        }
        pQueue->u16Numb--;
        bRet = TRUE;
    }
    return bRet;
}


